import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios"
import { ElMessage } from "element-plus"
import {ResponseData} from '../type/index'
import store from '../store/index'
import { UserInfo } from '../type/index'
// import { useStore } from "vuex"
// import { key } from '../store'
// const store = useStore()

// 创建 axios 实例
let instance: AxiosInstance | any
// import.meta.env.MODE === "development" 判断是生产还是开发环境
instance=axios.create({
    baseURL:"/api",
    timeout:50000
})
// request.interceptors.request.use(
//     function (config) {
//       if (!window.localStorage.getItem(headerTokenKey)) {
//         console.log('没有token')
//       } else {
//         // console.log(window.localStorage.getItem(headerTokenKey));
//         config.headers[headerTokenKey] = window.localStorage.getItem(headerTokenKey)
//       }
//       // console.log('====', config);
//       return config
//     },
//     function (error) {
//       return Promise.reject(error)
//     }
//   )
//请求拦截器
instance.interceptors.request.use(
    (config:AxiosRequestConfig)=>{
        // console.log(JSON.parse(window.localStorage.getItem('userInfo')).token)
        let token=window.localStorage.getItem('blogToken');
        // console.log("token",token)
        if(!token){
            // console.log('没有token')
        }else{
            // console.log(config)
            config.headers.common['Authorization'] = 'Bearer ' + token;
        }
        // JSON.parse(window.localStorage.getItem('userInfo'))
        // console.log(window.localStorage.getItem('userInfo'))
    //  if (!window.localStorage.getItem('userInfo')) {
    //     console.log('没有token')
    //   } else {
    //     // console.log(window.localStorage.getItem(headerTokenKey));
    //     config.headers[headerTokenKey] = window.localStorage.getItem(headerTokenKey)
    //   }
        // console.log(window.localStorage.getItem('userInfo'))
        // console.log(store.state.user.userInfo)
        // if (!window.localStorage.getItem()) {
        //     console.log('没有token')
        //   } else {
        //     // console.log(window.localStorage.getItem(headerTokenKey));
        //     config.headers[headerTokenKey] = window.localStorage.getItem(headerTokenKey)
        //   }
        //   // console.log('====', config);
        //   return config
        return config;},
   (error:any)=>{
       // Do something with request error
       console.error("error:", error); // for debug
       Promise.reject(error);
   }
)
//响应拦截器
instance.interceptors.response.use(
    // (res: AxiosResponse) => {
    //   // Some example codes here:
    //   // code == 0: success
    //   if (res.status === 200) {
    //     const data: ResponseData = res.data
    //     if (data.code === 0) {
    //       return data.data;
    //     } else {
    //       ElMessage({
    //         message: data.message,
    //         type: "error"
    //       });
    //     }
    //   } else {
    //     ElMessage({
    //       message: "网络错误!",
    //       type: "error"
    //     });
    //     return Promise.reject(new Error(res.data.message || "Error"));
    //   }
    // },
    // (error: any) => Promise.reject(error)
  );
  
  export default instance